﻿// Exception view form, (c)2009-2010 Marek Buchar, Knihovnik
// This file is licensed under LGPL license, see more at http://www.gnu.org/licenses/lgpl.html
namespace vKapse.Common
{

	#region Usingns

	using System;
	using System.Diagnostics;
	using System.Drawing;
	using System.Windows.Forms;

	#endregion Usings

	/// <summary>
	/// Exception view form
	/// </summary>
	public partial class ExceptionView : Form
	{

		#region Members

		private Exception m_Exception;

		#endregion

		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="e">Exception</param>
		public ExceptionView(Exception e)
		{
			InitializeComponent();

			m_Exception = e;
			this.picImage.Image = SystemIcons.Warning.ToBitmap();
			SetTitles();
		}

		#endregion

		#region Event Handling

		private void btnSend_Click(object sender, EventArgs e)
		{
			this.btnSend.Enabled = false;
			MethodInvoker invoker = new MethodInvoker(delegate
				{
					try
					{
						Process.Start(string.Format("http://code.google.com/p/fb-chat/issues/entry?template=Defect%20report%20from%20user&summary={0}",
							this.m_Exception.Message));
					}
					catch { /* TODO O:-) */ }
				});
			invoker.BeginInvoke(null, null);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		#endregion

		#region Methods

		public static void Show(Exception ex)
		{
			using (ExceptionView form = new ExceptionView(ex))
			{
				form.ShowDialog();
			}
		}

		/// <summary>
		/// Set strings from resource and exception
		/// </summary>
		private void SetTitles()
		{
			this.txtMessage.Text = m_Exception.Message;
			this.txtModule.Text = m_Exception.Source;
			this.txtFunction.Text = m_Exception.TargetSite.ToString();

			this.Text = FacebookChat.Properties.Resources.Error;
			this.lblException.Text = FacebookChat.Properties.Resources.UnhandledException;
			this.grpInfo.Text = FacebookChat.Properties.Resources.ExceptionInfo;
			this.lblMessage.Text = FacebookChat.Properties.Resources.ExceptionMessage;
			this.lblModule.Text = FacebookChat.Properties.Resources.ExceptionModule;
			this.lblFunction.Text = FacebookChat.Properties.Resources.ExceptionFunction;
			this.btnSend.Text = FacebookChat.Properties.Resources.ExceptionSend;
			this.btnCancel.Text = FacebookChat.Properties.Resources.Close;
		}

		#endregion

	}
}
